#include "TestScene1.h"

float rotation = 0;
float scalation = 0;
float translation = 0;
short coloration = 0;

float translationX = -200;
float translationY = 0;

const std::string WALK_RIGHT = "WalkRight";
const std::string WALK_LEFT = "WalkLeft";

/*-----------------------------------------------------------------------------------------------*/
TestScene1::TestScene1(std::string name) : Scene(name)
{
}
/*-----------------------------------------------------------------------------------------------*/
TestScene1::~TestScene1()
{
   delete m_pTriangle;
   delete m_pSquare;
}
/*-----------------------------------------------------------------------------------------------*/
bool TestScene1::onInit(Importer* pImporter)
{

   ////////////////////////////// <Shapes> ///////////////////////////////

   m_pTriangle = new Triangle("triangulo");
   m_pSquare = new Square("cuadrado");  

   addEntity(m_pTriangle);
   addEntity(m_pSquare);

   m_pTriangle->Scale(2,2);
   m_pTriangle->setVisible(false);
   
   m_pSquare->Scale(80,80);

   m_pSquare->setColor(0,0,255);

   ////////////////////////////// <Sprites> ///////////////////////////////

   if ( !pImporter->importResources("./Resources/Sprites.xml") )
   {
      std::cout << "Could not import resources" << std::endl;
      return false;
   }
   
   m_pWonderboy = new Sprite("wonderboy");

   if ( !pImporter->createSprite(m_pWonderboy->getName().c_str() , m_pWonderboy) )
   {
      std::cout << "Failed to create sprite: " << m_pWonderboy->getName() << std::endl;
      return false;
   }
   
   m_pWonderboy->Scale(100,100);
   m_pWonderboy->Translate(translationX,0,0);

   if ( !m_pWonderboy->setAnimation(WALK_LEFT) )
   {
      std::cout << "Failed to set animation " << WALK_LEFT << std::endl;  
      return false;
   }

   m_pWonderboy->getAnimation()->stop();

   m_pWonderboy->setMooving(true);

   if (!addEntity(m_pWonderboy))
   {
      std::cout << "Failed to add entity: " << m_pWonderboy->getName() << std::endl;
   }

   //////////////////////////////////////////////////////////////////////////
   //collisions
   //////////////////////////////////////////////////////////////////////////


   addToCollisionGrup(std::string("1"),m_pWonderboy);
   addToCollisionGrup(std::string("2"),m_pSquare);
   
   return true;
}
/*-----------------------------------------------------------------------------------------------*/
void TestScene1::onUpdate()
{
   ////////////////////////////// <Shapes> ///////////////////////////////
   translation += (translation < 500) ? 0.1f : -1000.0f;
   rotation += 0.1f;
   scalation += (scalation < 500) ? 0.05f : -500.0f;
   coloration += (scalation < 256) ? 1 : -255;

   //m_pSquare->setColor(0,0,coloration);
   //m_pSquare->Translate(translation);
   //m_pSquare->Rotate(0,0,rotation);
   //m_pSquare->Scale(scalation,scalation);

   ////////////////////////////// <Sprites> ///////////////////////////////

   if (getInput()->getKeyDown(DIK_UP))
   {
      translationY += 0.3;
   }
   if (getInput()->getKeyDown(DIK_DOWN))
   {
      translationY -= 0.3;
   }
   if (getInput()->getKeyDown(DIK_LEFT))
   {
      translationX -= 0.3;
   }
   if (getInput()->getKeyDown(DIK_RIGHT))
   {
      translationX += 0.3;
   }

   if (getInput()->getKeyEventDown(DIK_RIGHT))
   {
      if (m_pWonderboy->setAnimation(WALK_RIGHT))
      {
         m_pWonderboy->getAnimation()->play();
      }
   }
   
   if (getInput()->getKeyEventDown(DIK_LEFT))
   {
     if (m_pWonderboy->setAnimation(WALK_LEFT))
      {
         m_pWonderboy->getAnimation()->play();
     }
   }

   if (getInput()->getKeyEventDown(DIK_SPACE))
   {
      m_pWonderboy->setAnimation("JumpLeft");
   }

   m_pWonderboy->Translate(translationX,translationY,0);  
}
/*-----------------------------------------------------------------------------------------------*/
void TestScene1::onDraw()
{
   
}